<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ExhibitionVideo extends BaseModel
{
    use HasFactory;


    protected $table = 'exhibition_video';

    /**
     * 获取展览对应作品
     */
    public function video(){
        return $this->belongsTo('App\Models\ExhibitionVideo' , 'video_id');
    }

     /**
     * 获取所有的作品id
     * @param exhibition_ids 展览id  string|array
     */
    public function getVideoIdByExhibitionId($exhibition_ids){
        if(gettype($exhibition_ids) !== 'array' ){
            $exhibition_ids = explode(',' , $exhibition_ids);//转化和为数组
        }

        return $this->whereIn('exhibition_id' , $exhibition_ids)->orderByDesc('id')->pluck('video_id');
    }

     /**
     * 展览删除作品
     * @param $exhibition_id 展览id
     * @param $video_ids 作品id
     */
    public function exhibitionVideoDel($exhibition_id , $video_ids){
        $video_ids = explode(',' , $video_ids);
        return $this->whereIn('video_id' , $video_ids)->where('exhibition_id' , $exhibition_id)->delete();
    }
     /**
     * 展览添加作品
     * @param $exhibition_id 展览id
     * @param $video_ids 作品id
     */
    public function exhibitionVideoAdd($exhibition_id , $video_ids){
        $video_ids = explode(',' , $video_ids);
        foreach($video_ids as $key=>$val){
            $data[$key]['video_id']= $val;
            $data[$key]['exhibition_id']= $exhibition_id;
            $data[$key]['create_time']= date('Y-m-d H:i:s');
        }
        return $this->insert($data);
    }



    
     /**
     * 判断此作品是否在展览中
     * @param id 展览id
     */
    public function videoIsExhibition($id){
        $exhibition_id = $this->where('video_id' , $id)->value('exhibition_id');
        if($exhibition_id){
            return  Exhibition::where('id' , $exhibition_id)->where('is_del' ,1)->value('name');
        }

        return false;//未存在在任何展览中 
    }

    /**
     * 添加展览作品，删除多余的，增加新增的
     * @param exhibition_id 展览id
     * @param video_id 作品id  多个 逗号 拼接
     * @param $type 类型  add 新增，直接增加   change 需要查询
     */
    public function exhibitionVideoChange($exhibition_id , $video_ids , $type = 'change'){
        $video_ids = empty($video_ids) ? [] : explode(',' , $video_ids);
        if($type == 'add'){
            $video_ids_ed = [];
        }else{
            $video_ids_ed = $this->getVideoIdByExhibitionId($exhibition_id)->toArray();
        }
        $video_ids_del = array_diff($video_ids_ed,$video_ids); //对比返回在 $video_ids_ed 中但是不在 $video_ids 的数据删除
        $video_ids_add = array_diff($video_ids,$video_ids_ed); //对比返回在 $video_ids 中但是不在 $video_ids_ed 的数据添加
        $this->whereIn('video_id' , $video_ids_del)->where('exhibition_id' , $exhibition_id)->delete();
        $data = [];
        foreach($video_ids_add as $key=>$val){
            $data[$key]['exhibition_id'] = $exhibition_id; 
            $data[$key]['video_id'] = $val; 
            $data[$key]['create_time'] = date('Y-m-d H:i:s');
        }
        $this->insert($data);

    }




}
